﻿Public Class CSourceFolder : Inherits CSourceAbstract

#Region "Constructor"
    Public Sub New(folderPath As String)
        m_folderPath = folderPath
    End Sub
#End Region

#Region "Member"
    Private m_folderPath As String
#End Region

#Region "Overrides"
    Public Overrides ReadOnly Property Name As String
        Get
            Return m_folderPath
        End Get
    End Property

    'Get Info
    'Public Overrides Function GetKeys() As List(Of String)
    '    LogReadStart()
    '    GetKeys = Keys(m_folderPath)
    '    LogReadFinish(GetKeys.Count)
    'End Function
    Public Overrides Function GetKeysAndSizes() As CKeysAndSizes
        LogReadStart()
        Dim keys As CKeysAndSizes = KeysAndSizes(m_folderPath)
        LogReadFinish(keys.Count, keys.Total)
        Return keys
    End Function

    'Make Changes
    Protected Overloads Overrides Sub CopyTo(destination As CSourceFolder, key As String)
        Dim fromPath As String = FullPath(key)
        Dim toPath As String = destination.FullPath(key)
        Dim toFolder As String = IO.Path.GetDirectoryName(toPath)
        If Not IO.Directory.Exists(toFolder) Then IO.Directory.CreateDirectory(toFolder)

        IO.File.Copy(fromPath, toPath)
    End Sub
    Protected Overloads Overrides Sub CopyTo(destination As CSourceS3, key As String)
        key = key.Replace("\", "/")
        Try
            CAmazonS3.PutObject(destination.Client, destination.CurrentBucket, FullPath(key), key)
        Catch ex As Exception
            If ex.Message.ToLower.Contains("remote name could not be resolved") Then
                Log("Internet down - waiting 30secs...", vbTab, CUtilities.LongDateTime(DateTime.Now))
                System.Threading.Thread.Sleep(30000)
                CopyTo(destination, key) 'Wait then recurse
            Else
                Throw ex
            End If
        End Try
    End Sub
#End Region

#Region "Helpers"
    Public Function FullPath(key As String) As String
        Return String.Concat(m_folderPath, "\", key)
    End Function
#End Region

#Region "Shared: List/Dict"
    'Public Shared Function Keys(folderPath As String) As List(Of String)
    '    Dim paths As List(Of String) = FilePathsRecursive(folderPath)
    '    Dim chop As Integer = folderPath.Length
    '    If Not folderPath.EndsWith("/") AndAlso Not folderPath.EndsWith("\") Then chop += 1
    '    Dim list As New List(Of String)(paths.Count)
    '    For Each i As String In paths
    '        list.Add(i.Substring(chop).ToLower)
    '    Next
    '    Return list
    'End Function
    Public Shared Function KeysAndSizes(folderPath As String) As CKeysAndSizes
        Dim paths As List(Of String) = FilePathsRecursive(folderPath)
        Dim chop As Integer = folderPath.Length
        If Not folderPath.EndsWith("/") AndAlso Not folderPath.EndsWith("\") Then chop += 1
        Dim list As New CKeysAndSizes(paths.Count)
        For Each i As String In paths
            list.Add(i.Substring(chop).ToLower, New IO.FileInfo(i).Length)
        Next
        Return list
    End Function
#End Region


#Region "Shared: FileSystem Recursion"
    Public Shared Function FilePathsRecursive(folderPath As String) As List(Of String)
        Dim list As New List(Of String)
        FilePathsRecursive(folderPath, list)
        Return list
    End Function
    Public Shared Sub FilePathsRecursive(folderPath As String, list As List(Of String))
        If folderPath.Contains("$RECYCLE.BIN") Then Exit Sub
        'If folderPath.Contains(" ") Then Exit Sub
        list.AddRange(IO.Directory.GetFiles(folderPath))
        For Each subFolder As String In IO.Directory.GetDirectories(folderPath)
            FilePathsRecursive(subFolder, list)
        Next
    End Sub
#End Region

End Class
